package mspdt.ubzuuch.mn;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
      
	private static final String DATABASE_NAME = "ubzuuch.db";
	   private static final int DATABASE_VERSION = 1;
	   private static final String TABLE_NAME = "datas";
        
    private final Context context;  
    
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
        	db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER, title TEXT, body TEXT, price TEXT, list_image TEXT, roomimages TEXT, bairimages TEXT, " +
        			"tsetserleg TEXT, dundsurguuli TEXT, ihsurguuli TEXT, lat TEXT, lan TEXT, street_image TEXT, video TEXT, contact_us TEXT, category TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            db.execSQL("DROP TABLE IF EXISTS contents");
            onCreate(db);
        }
    }  
    
  //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }
//---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    public void saveData(int id, String title, String body, String price, String list_image, String roomimages, String bairimages, String tsetserleg, String dundsurguuli,
    		String ihsurguuli, String lat, String lan, String street_image, String video, String contact_us, String category)
    {
    	db.execSQL("INSERT INTO "+TABLE_NAME+"(id, title, body, price, list_image, roomimages, bairimages, tsetserleg, dundsurguuli, ihsurguuli, lat, lan, street_image, video, contact_us, category) " +
    			"values ("+id+", '"+title+"', '"+body+"', '"+price+"', '"+list_image+"', '"+roomimages+"', '"+bairimages+"', '"+tsetserleg+"', '"+dundsurguuli+"', '"+ihsurguuli+"', '"+lat+"', " +
    					"'"+lan+"', '"+street_image+"', '"+video+"', '"+contact_us+"', '"+category+"')");
    }
    public boolean isSaved(int id)
    {
    	boolean isSaved = false;
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"id"}, null, null, null, null, null);
    	if(cursor.moveToFirst())
    	{
    		do
    		{
    			if(id==cursor.getInt(0))
    			{
    				isSaved = true;
        			break;
    			}
    		}
    		while(cursor.moveToNext());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return isSaved;
    }
    public ArrayList<Integer> getIds()
    {
    	ArrayList<Integer> values = new ArrayList<Integer>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getInt(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    public ArrayList<String> getCategories()
    {
    	ArrayList<String> values = new ArrayList<String>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"category"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getString(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    public ArrayList<String> getTitles()
    {
    	ArrayList<String> values = new ArrayList<String>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"title"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getString(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    public ArrayList<String> getBodys()
    {
    	ArrayList<String> values = new ArrayList<String>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"body"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getString(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    public ArrayList<String> getPrices()
    {
    	ArrayList<String> values = new ArrayList<String>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"price"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getString(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    public ArrayList<String> getListImages()
    {
    	ArrayList<String> values = new ArrayList<String>();
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"list_image"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			values.add(cursor.getString(0));				
    		}
    		while(cursor.moveToPrevious());
    	}
    	if(cursor != null || !cursor.isClosed())
    		cursor.close();
    	return values;
    }
    
    
    public String getRoomImages(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"roomimages", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getPrice(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"price", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getTitle(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"title", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getBody(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"body", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getBairImages(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"bairimages", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getTsetserleg(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"tsetserleg", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getDundSurguuli(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"dundsurguuli", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }   
    public String getIhSurguuli(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"ihsurguuli", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }   
    public String getLat(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"lat", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getLan(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"lan", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getStreetImage(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"street_image", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }    
    public String getVideo(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"video", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getContact(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"contact_us", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }
    public String getCategory(int id)
    {
    	String room_images = "";
    	Cursor cursor = this.db.query(TABLE_NAME, new String[] {"category", "id"}, null, null, null, null, null);
    	if(cursor.moveToLast())
    	{
    		do
    		{
    			if(id==cursor.getInt(1))
    			{
    				room_images=cursor.getString(0);
    				break;
    			}
    		}
    		while(cursor.moveToPrevious());
    	}
    	return room_images;
    }


    public void deleteData(int id)
    {
    	db.execSQL("DELETE FROM "+TABLE_NAME+" WHERE id="+id);
    }


}
